<?php
$pageTitle = "排行榜";
$currentPage = "rank";
include('../resource/db/db.php');
include('../resource/navbar.php');

// 获取排行榜数据
$sql = "SELECT sets.*, users.username AS creator 
        FROM sets 
        JOIN users ON sets.user_id = users.user_id 
        WHERE sets.is_deleted=FALSE AND sets.is_copy=0 
        ORDER BY reward DESC";
$result = $conn->query($sql);

// 检查用户是否已登录
$user_id = isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['add_to_set'])) {
        $set_id = $_POST['set_id'];
        // 获取卡组信息
        $set_sql = "SELECT * FROM sets WHERE set_id='$set_id' AND is_deleted=FALSE";
        $set_result = $conn->query($set_sql);
        $set = $set_result->fetch_assoc();

        // 添加卡组到用户的卡组
        $new_set_name = $set['name'] . " - 复制";
        $insert_set_sql = "INSERT INTO sets
        (name, user_id, info, created_at, is_deleted, reward, is_copy)
        VALUES ('$new_set_name', '$user_id', '{$set['info']}', NOW(), 0, 0, 1)";
        if ($conn->query($insert_set_sql)) {
            // 获取新卡组的ID
            $new_set_id = $conn->insert_id;
            $insert_cards_sql = "INSERT INTO cards
            (title, content, set_id, user_id, card_id, created_at, is_deleted, updated_at) 
            SELECT title, content, '$new_set_id', '$user_id',  card_id, NOW(), 0, NOW()
            FROM cards
            WHERE set_id='$set_id' AND is_deleted=FALSE";
            if ($conn->query($insert_cards_sql)) {
                $message = "卡组已成功添加到您的卡组中。";
            } else {
                $message = "添加卡片时出错：" . $conn->error;
            }
        } else {
            $message = "添加卡组时出错：" . $conn->error;
        }
    } elseif (isset($_POST['reward'])) {
        $set_id = $_POST['set_id'];
        // 增加热度
        $reward_sql = "UPDATE sets SET reward=reward+1 WHERE set_id='$set_id'";
        if ($conn->query($reward_sql)) {
            $message = "谢谢你的喜欢！热度+1。";
        } else {
            $message = "打赏失败，请重试：" . $conn->error;
        }
    }
}
?>

<!DOCTYPE html>
<html>

<head>
    <title>排行榜</title>
    <link rel="stylesheet" href="../resource/css/styles.css">
    <style>
        .set-info {
            display: flex;
            flex-direction: column;
        }

        .list-group-item {
            cursor: pointer;
        }

        .list-group-item:hover {
            background-color: #f0f0f0;
        }

        .set-link {
            text-decoration: none;
            color: inherit;
        }

        .count {
            display: flex;
            justify-content: right;
        }

        .set-form {
            margin-top: 10px;
        }
    </style>
    <script>
        function goToSetPage(setId) {
            window.location.href = 'people-set.php?set_id=' + setId;
        }
    </script>
</head>

<body>
    <div class="container">
        <h2>热门卡组排行榜</h2>
        <?php if (isset($message)) : ?>
            <div class="alert alert-info"><?php echo $message; ?></div>
        <?php endif; ?>
        <ul class="list-group">
            <?php
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    // 展示卡组信息和卡片总数
                    $set_id = $row['set_id'];
                    $set_sql = "SELECT * FROM cards WHERE set_id='$set_id' AND is_deleted=FALSE";
                    $set_result = $conn->query($set_sql);
                    $set_count = $set_result->num_rows;

                    echo "<li class='list-group-item' onclick='goToSetPage(\"" . $set_id . "\")'>";
                    echo "<div class='set-info'>";
                    echo "<a href='people-set.php?set_id=" . $set_id . "' class='set-link'>";
                    echo "<h3>" . htmlspecialchars($row['name']) . "</h3></a>";
                    echo "<div class='info'>" . htmlspecialchars($row['info']) . "</div>";
                    echo "<div class='count'>卡片数量: " . $set_count . "</div>";
                    echo "<div class='count'>热度: " . $row['reward'] . "</div>";
                    echo "<div class='count'>创建者: " . htmlspecialchars($row['creator']) . "</div>";
                    echo "</div>";

                    if ($user_id) {
                        echo "<div class='set-form'>";
                        echo "<form method='post'>";
                        echo "<input type='hidden' name='set_id' value='" . $set_id . "'>";
                        echo "<button type='submit' name='add_to_set' class='btn btn-primary'>添加到我的卡组</button>";
                        echo "<button type='submit' name='reward' class='btn btn-success'>打赏</button>";
                        echo "</form>";
                        echo "</div>";
                    }

                    echo "</li>";
                }
            } else {
                echo "<li class='list-group-item'>暂无数据</li>";
            }
            ?>
        </ul>
    </div>
</body>

</html>